<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" @submit.native.prevent v-show="showSearch" label-width="68px">
      <el-form-item label="物料编码" prop="productNo">
        <el-input
          v-model="queryParams.productNo"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="容器编码" prop="containerNo">
        <el-input
          v-model="queryParams.containerNo"
          placeholder="请输入容器编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="托盘" prop="trayNo">
        <el-input
          v-model="queryParams.trayNo"
          placeholder="请输入托盘"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="批次号" prop="batchNo">
        <el-input
          v-model="queryParams.batchNo"
          placeholder="请输入批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库位编号" prop="storehouseLocationNo">
        <el-input
          v-model="queryParams.storehouseLocationNo"
          placeholder="请输入库位编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="请选择库存状态"  >
          <el-option
            v-for="dict in dict.type.sh_stock_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="info"-->
<!--          plain-->
<!--          icon="el-icon-upload2"-->
<!--          size="mini"-->
<!--          @click="handleImport"-->
<!--          v-hasPermi="['inventory:stock:import']"-->
<!--        >初始化导入</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['inventory:stock:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="businessList" @selection-change="handleSelectionChange">
      <af-table-column label="物料编码" align="center" prop="productNo" min-width="150" show-overflow-tooltip />
      <af-table-column label="物料名称" align="center" prop="productName" min-width="150" show-overflow-tooltip />
      <el-table-column label="容器" align="center" prop="containerNo" width="100" show-overflow-tooltip />
      <el-table-column label="托盘" align="center" prop="trayNo" width="100" show-overflow-tooltip />
      <af-table-column label="库位编号" align="center" prop="storehouseLocationNo" width="120" show-overflow-tooltip />
      <af-table-column label="库区编号" align="center" prop="storehouseAreaNo" width="120" show-overflow-tooltip />
      <af-table-column label="仓库编号" align="center" prop="storehouseNo" width="120" show-overflow-tooltip />
      <af-table-column label="库存数量" align="center" prop="qty" width="80" show-overflow-tooltip />
      <el-table-column label="单位" align="center" prop="company" width="60" show-overflow-tooltip />
      <af-table-column label="批次号" align="center" prop="batchNo" width="120" show-overflow-tooltip />
      <el-table-column label="库存状态" align="center" prop="stockStatus" width="80" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_stock_status" :value="scope.row.stockStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="库存状态" align="center" prop="status" width="80" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sh_stock_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="生产日期" align="center" prop="productionDate" width="155" show-overflow-tooltip />-->
      <af-table-column label="有效天数" align="center" prop="validDateNum" width="80" show-overflow-tooltip />
<!--      <el-table-column label="有效期" align="center" prop="effectiveDate" width="155" show-overflow-tooltip />-->
<!--      <el-table-column label="订单号" align="center" prop="orderNo" width="150" show-overflow-tooltip />-->
<!--      <el-table-column label="创建人" align="center" prop="createBy" width="80" show-overflow-tooltip />-->
<!--      <el-table-column label="创建时间" align="center" prop="createTime"  width="152">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.createTime) }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="最后更新人" align="center" prop="updateBy"  min-width="100"/>-->
<!--      <el-table-column label="最后更新时间" align="center" prop="createTime"  width="152">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.updateTime) }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="no">
          <el-input v-model="form.no" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="仓库编号" prop="storehouseNo">
          <el-input v-model="form.storehouseNo" placeholder="请输入仓库编号" />
        </el-form-item>
        <el-form-item label="库区编号" prop="storehouseAreaNo">
          <el-input v-model="form.storehouseAreaNo" placeholder="请输入库区编号" />
        </el-form-item>
        <el-form-item label="库位编号" prop="storehouseLocationNo">
          <el-input v-model="form.storehouseLocationNo" placeholder="请输入库位编号" />
        </el-form-item>
        <el-form-item label="物料编码" prop="productNo">
          <el-input v-model="form.productNo" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="物料名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="物料批次" prop="batchNo">
          <el-input v-model="form.batchNo" placeholder="请输入物料批次" />
        </el-form-item>
        <el-form-item label="库存数量" prop="qty">
          <el-input v-model="form.qty" placeholder="请输入库存数量" />
        </el-form-item>
        <el-form-item label="单位" prop="company">
          <el-input v-model="form.company" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="是否盘库锁定" prop="isCheckLock">
          <el-input v-model="form.isCheckLock" placeholder="请输入是否盘库锁定" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="是否删除：1是 0否" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入是否删除：1是 0否" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--          <div class="el-upload__tip" slot="tip">-->
          <!--            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据-->
          <!--          </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listBusiness, getBusiness, delBusiness, addBusiness, updateBusiness } from "@/api/storehouse/stock/stock";
import { getToken } from "@/utils/auth";

export default {
  dicts: ['sh_stock_status','stock_quality_status','erp_stock_status'],
  name: "Business",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存表格数据
      businessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        storehouseNo: null,
        storehouseAreaNo: null,
        storehouseLocationNo: null,
        barcode: null,
        productNo: null,
        productVersion: null,
        productName: null,
        batchNo: null,
        qty: null,
        company: null,
        status: null,
        isCheckLock: null,
      },
      // 导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的物料数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/storehouse/stock/importExcel"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        no: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
        storehouseNo: [
          { required: true, message: "仓库编号不能为空", trigger: "blur" }
        ],
        storehouseAreaNo: [
          { required: true, message: "库区编号不能为空", trigger: "blur" }
        ],
        storehouseLocationNo: [
          { required: true, message: "库位编号不能为空", trigger: "blur" }
        ],
        qty: [
          { required: true, message: "库存数量不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "库存状态正常：NORMAL不能为空", trigger: "blur" }
        ],
        isCheckLock: [
          { required: true, message: "是否盘库锁定不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "是否删除：1是 0否不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询库存列表 */
    getList() {
      this.loading = true;
      listBusiness(this.queryParams).then(response => {
        this.businessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        storehouseNo: null,
        storehouseAreaNo: null,
        storehouseLocationNo: null,
        barcode: null,
        productNo: null,
        productVersion: null,
        productName: null,
        batchNo: null,
        qty: null,
        company: null,
        status: "0",
        isCheckLock: null,
        remark: null,
        delFlag: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "库存明细导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      const a = document.createElement('a');
      a.href = `${process.env.BASE_URL}static/file/库存管理导入模板.xlsx`
      a.click();
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库存";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBusiness(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库存";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBusiness(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBusiness(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('deleteTip', { num: ids.length })).then(function() {
        return delBusiness(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('storehouse/stock/export', {
        ...this.queryParams,ids: this.ids
      }, `库存管理_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
